package com.tougutips.like.mq;

import com.rabbitmq.client.Channel;
import com.tougutips.like.dto.MessageDto;
import com.tougutips.like.entity.LikeAll;
import com.tougutips.like.mapper.LikeMapper;
import com.tougutips.like.service.LikeService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * @author 张治怡
 * @version 1.0
 * @description 消息通知处理
 * @dateTime 2022/7/25 10:52
 */

@Component
public class MessageReceiver {
    @Autowired
    private LikeMapper likeMapper;

    @RabbitListener(queues = "messageQueue")
    public void messageReceived(Channel channel, Message message, MessageDto messageDto) {
        try {
            // 持久化点赞数据
            LikeAll likeAll = new LikeAll();
            BeanUtils.copyProperties(messageDto,likeAll);
            likeAll.setUpdateTime(new Date());
            likeAll.setCreateTime(new Date());
            likeAll.setIsDeleted(1);
            likeMapper.insert(likeAll);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
